package com.lanyun.controller;/**
 * @author zhy
 * @date 2021/1/6 - 11:58
 */

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lanyun.pojo.Admin;
import com.lanyun.pojo.Function;
import com.lanyun.pojo.Privilege;
import com.lanyun.pojo.Role;
import com.lanyun.service.AdminService;
import com.lanyun.service.FunctionService;
import com.lanyun.service.PrivilegeService;
import com.lanyun.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**

 @ClassName RoleController
 @Description xxx
 @Author zhy
 @Date 2021/1/6 - 11:58
 @Version 1.0
 **/
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @Autowired
    private AdminService adminService;
    
    @Autowired
    private PrivilegeService privilegeService;
    
    @Autowired
    private FunctionService functionService;

    @RequestMapping("/showRole")
    @ResponseBody
    public Object showRole() throws JsonProcessingException {
        List<Role> roleList = roleService.getRole();
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(roleList);
        System.out.println(json);
        return roleList;
    }

    @RequestMapping("/showAll")
    public String showAll(Model model){//--查询所有角色，封装了不同角色人数和不同角色所有管理员名字
        List<Role> roleList = roleService.getRole();
        Map<Integer , String> realNameMap = new HashMap<Integer , String>();//key--角色id，value-该角色的所有管理员名字
        Map<Integer , Integer> counts = new HashMap<>();//key--角色id，value--该角色的管理员人数
        model.addAttribute("allrole",roleList);
        for (int i = 0; i < roleList.size(); i++) {
            StringBuilder sb = new StringBuilder();
            List<Admin> adminList = adminService.findAdminById(roleList.get(i).getRid());
            if (adminList.size()==0){
                counts.put(roleList.get(i).getRid(),0);
                realNameMap.put(roleList.get(i).getRid(),"暂时还没有员工任职该角色！");
            }else {
                counts.put(adminList.get(0).getRole().getRid(), adminList.size());
                for (int j = 0; j < adminList.size(); j++) {
                    if ((adminList.size() - 1) == j) {
                        sb.append(adminList.get(j).getName());
                    } else {
                        sb.append(adminList.get(j).getName()).append(",");
                    }
                }
                realNameMap.put(adminList.get(0).getRole().getRid(), sb.toString());
            }
        }
        model.addAttribute("map",realNameMap);
        model.addAttribute("count",counts);
        model.addAttribute("size",realNameMap.size());
        return "rolepage";
    }

    @RequestMapping("/addRole")
    public String addRole(Role role){//新增角色
        System.out.println("新增角色拿到的角色信息为：  --  "+role);
        Integer result = roleService.insertRole(role);
        if (result == 1){
            return "redirect:/role/showAll";
        }
        return "";
    }

    @RequestMapping("/del")
    @ResponseBody
    public Object del(Integer id){//根据角色id删除角色
        System.out.println("删除角色拿到的id是：  -- " + id);
        Integer result = roleService.deleteRole(id);
        if (result == 1){
            return result;
        }
        return "";
    }

    @RequestMapping("/delSome")
    @ResponseBody
    public Object delSome(String ids){
        System.out.println("批量删除角色拿到的id为：  -  " + ids);
        Integer result = roleService.deleteSomeRole(ids);
        if (result>=1){
            return result;
        }
        return "";
    }

    /*List<Integer> list;//------权限id集合
    List<Privilege> privilegeList;//-----权限对象集合
    @RequestMapping("/jump")
    public String jump(Integer id , Model model){
        System.out.println("页面跳转传递的角色id为：-- "+id);
        list = roleService.getPrivilege(id);//该角色的权限id集合
        privilegeList = privilegeService.findPriviById(list);//该角色的权限对象集合
        Map<Integer , List<Function>> functionMap = new HashMap();
        for (int i = 0; i < privilegeList.size(); i++) {
            List<Function> functionList = functionService.findFunction(privilegeList.get(i).getPid());
            functionMap.put(privilegeList.get(i).getPid(),functionList);
        }
        model.addAttribute("allPrivilege",privilegeList);
        model.addAttribute("allFunction",functionMap);
        return "privilege_update";
    }*/

    List<Integer> list;//------权限id集合
    List<Privilege> privilegeList;//-----权限对象集合
    @RequestMapping("/updatePrivilege")
    @ResponseBody
    public Object updatePrivilege(Integer id){
        System.out.println("要修改权限拿到的角色id为： -- "+id);
        list = roleService.getPrivilege(id);//该角色的权限id集合
        System.out.println("----"+list);
        if (list.size()==0){
            return "null";
        }else {
            privilegeList = privilegeService.findPriviById(list);//该角色的权限对象集合
            return privilegeList;
        }
    }

    List<Integer> list1;//------权限id集合
    List<Privilege> privilegeList1;//-----权限对象集合
    @RequestMapping("/default")
    @ResponseBody
    public Object defaultPri(Integer id){
        System.out.println("做修改权限数据回显默认角色id为： -- "+id);
        list1 = roleService.getPrivilege(id);//该角色的权限id集合
        privilegeList1 = privilegeService.findPriviById(list1);//该角色的权限对象集合
        return privilegeList1;
    }

    @RequestMapping("/updateEnd")
    @ResponseBody
    public Object updateEnd(String ids,Integer rid){//拿到提交的权限id 和 要修改的角色，进行权限修改
        System.out.println(ids+"----"+rid);
        Integer result = roleService.deletePrivilegeById(rid);
        Integer end = null;
//        if (result>=1){
            String id = ids.substring(0,ids.length()-1);
            String [] array = id.split(",");
            List<Integer> list = new ArrayList<>();
            for (int i = 0; i < array.length; i++) {
                list.add(Integer.parseInt(array[i]));
            }
            for (int i = 0; i < list.size(); i++) {
                end = roleService.addPrivilege(rid , list.get(i));
                end++;
            }
//        }
        if (end>=1){
            return end;
        }
        return "";
    }
}
